import requests
import xlwt
import re
import json
'''获取buglist
1.登陆接口，账号密码，返回token,拿到token

https://www.tapd.cn/api/entity/bugs/bugs_list
2.请求buglist,传参 bug标题 搜索相关需求bug,返回buglist

3.判断total_count总数，大于50条，翻页请求第二页内容

4.写入excel'''
'''
url='https://www.tapd.cn/api/entity/bugs/bugs_list'
data={
'confIdType': "CACHE",
'conf_id': "1160236733001012342",
'dsc_token': "NwTietgpBe5LP0MC",
'filter_expr': {
    'data':[
        {
            'fieldDisplayName': "标题",
            'fieldIsSystem': "1",
            'fieldOption': "like",
            'fieldSystemName': "name",
            'fieldType': "input",
            'selectOption': [],
            'value': "股票转入"
          },
        {
            'fieldDisplayName': "迭代",
            'fieldIsSystem': "1",
            'fieldOption': "in",
            'fieldSystemName': "iteration_id",
            'fieldType': "select",
            'selectOption': [],
            'value': []
        },
        {
            'fieldDisplayName': "状态",
            'fieldIsSystem': "1",
            'fieldOption': "in",
            'fieldSystemName': "status",
            'fieldType': "multi_select",
            'selectOption': [],
            'value': []
        },
        {
            'fieldDisplayName': "优先级",
            'fieldIsSystem': "1",
            'fieldOption': "in",
            'fieldSystemName': "priority",
            'fieldType': "select",
            'selectOption': [],
            'value': []
        },
    ],
    'needInit': 'true',
    'optionType': "AND"

                },
'order': "",
'page': 1,
'perpage': 50,
'return_url': "https://www.tapd.cn/tapd_fe/60236733/bug/list",
'selected_workspace_ids': [],
'sort_name': "",
'workspace_id': "60236733",
}
header={
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Content-Length': '920',
    'Content-Type': 'application/json;charset=UTF-8',
    'Cookie': 'tapdsession=1654181790bd4059e7ae5bec221e55368410bf1cbef2f733ca3ac7fe989418b45e00b0d544; __root_domain_v=.tapd.cn; _qddaz=QD.792354181791846; t_u=88960e3f2342d039751def302bd6b12b7f5b1430ce77312a7c3d9f86c99f956825a9c379b4e8be97276eea4bea5994128046612f6285ac4086d3c01c23586305f83660f0941ac918%7C1; _t_uid=556980368; _t_crop=50309185; tapd_div=101_186; dsc-token=NwTietgpBe5LP0MC; cloud_current_workspaceId=60236733',
    'Host': 'www.tapd.cn',
   'Origin': 'https://www.tapd.cn',
    'Referer': 'https://www.tapd.cn/tapd_fe/60236733/bug/list?confId=1160236733001012342',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
buglist=requests.post(url=url,json=data,headers=header)
print(buglist.json())'''
with open('C:\\Users\\Administrator\\PycharmProjects\\test_report\\buglist.txt',mode='r',encoding='utf-8')as f:
    bb=f.read()
    buglist=eval(bb)
# print(buglist)
# print(buglist.get('data').get('bugs_list'))
bglists=(buglist.get('data').get('bugs_list'))
print(bglists)
# bug=[]
severity_dict={'fatal':'致命','serious':'严重','normal':'一般','prompt':'提示','advice':'建议'}
priority_dict={'high':'高','medium':'中','low':'低','urgent':'紧急','insignificant':'无关紧要'}
# status_dict={}
import xlwt

# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding = 'utf-8')
# 创建一个sheet
worksheet = workbook.add_sheet('buglist')#,cell_overwrite_ok=True
for i in range(8):
    worksheet.col(i).width=20*256
# 写入excel，write(row_index,col_index,value)参数对应 行, 列, 值
#worksheet.write(1,0,'this is test')
# 保存（保存后在目录下新增xxx.xls文件）
#workbook.save('d:\\Excel_test.xls')
worksheet.write(0, 0, '标题')
worksheet.write(0, 1, '发现版本')
worksheet.write(0, 2, '严重程度')
worksheet.write(0, 3, '优先级')
worksheet.write(0, 4, '状态')
worksheet.write(0, 5, '处理人')
worksheet.write(0, 6, '创建人')
worksheet.write(0, 7, '创建时间')

# with open('C:\\Users\\Administrator\\PycharmProjects\\test_report\\report.xlsx','wb')as report:  #buglists
j=0
for i in bglists:
    # j=0
    print(j)
    # bug.append(i.get('Bug').get('title'))
    title=i.get('Bug').get('title')
    # status_alias=i.get('Bug').get('status_alias')
    # print(i['Bug']['title'])
    version_report=i.get('Bug').get('version_report') #版本
    status_alias=i.get('Bug').get('status_alias') #bug状态 status,bug状态码    status=i.get('Bug').get('status')#状态
    severity=severity_dict.get(i.get('Bug').get('severity')) #严重程度
    priority=priority_dict.get(i.get('Bug').get('priority'))#优先级
    current_owner=i.get('Bug').get('current_owner')#处理人
    reporter=i.get('Bug').get('reporter')#创建人
    created=i.get('Bug').get('created')#创建时间
    worksheet.write(1+j, 0, title)
    worksheet.write(1 + j, 1, version_report)
    worksheet.write(1 + j, 2, severity)
    worksheet.write(1 + j, 3, priority)
    worksheet.write(1 + j, 4, status_alias)
    worksheet.write(1 + j, 5, current_owner)
    worksheet.write(1 + j, 6, reporter)
    worksheet.write(1 + j, 7, created)
    j=j+1

worksheet = workbook.add_sheet('test_case')
workbook.save('C:\\Users\\Administrator\\PycharmProjects\\test_report\\test.xls')







